﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using AutoplanetaTaskManager.Daos;
using AutoplanetaTaskManager.Daos.EntityFramework;
using AutoplanetaTaskManager.Models;
using AutoplanetaTaskManager.Security;
using AutoplanetaTaskManager.Views;
using AutoplanetaTaskManager.Views.Admin;
using AutoplanetaTaskManager.Views.Components;

namespace AutoplanetaTaskManager
{
    public partial class MainViewForm : Form
    {
        private IPostDao _postDao = new PostDao();
        private List<Post> _posts;

        public MainViewForm()
        {
            InitializeComponent();
        }

        private void MainViewForm_Load(object sender, EventArgs e)
        {
            new AuthForm(this).ShowDialog();
        }

        private void updateButton_Click(object sender, EventArgs e)
        {
            SetPostItemsContent();
        }

        public void SetPostItemsContent()
        {
            postsFlowLayoutPanel.Hide();
            postsFlowLayoutPanel.Controls.Clear();

            _posts = _postDao.Load();
            foreach (Post post in _posts)
            {
                PostItemComponent postItemComponent = new PostItemComponent(post.Id);
                postItemComponent.SetContent(mainMonthCalendar.SelectionStart);
                postsFlowLayoutPanel.Controls.Add(postItemComponent);
            }
            postsFlowLayoutPanel.Show();
        }

        private void выйтиToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SecurityManager.Get().User = null;
            SecurityManager.Get().Role = null;
            new AuthForm(this).ShowDialog();
        }

        private void закрытьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        private void сотрудникиToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (SecurityManager.Get().Role.Name == "ADMIN")
                new EmployeesManageForm().ShowDialog();
            else
                MessageBox.Show("Необходимы права уровня АДМИНИСТРАТОР", "Ошибка входа!");
        }
    }
}
